﻿using DotNetExtensions.AspNet;
using DotNetExtensions.AspNet.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace AspNetSimple
{
    public class CHSI_Simple
    {
        //public CHSI_Simple()
        //{
        //    var client = HttpClientFactory.Default.Create(HttpClientFactory.HttpClientKind.Restful);

        //    List<KeyValue> keyValues = new List<KeyValue>()
        //    {
        //        new KeyValue("mldm","zyxw"),
        //        new KeyValue("yjxkdm","0860")
        //    };

        //    byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyValues));
        //    var headers = new Header[] { new Header("Content-Type", "application/x-www-form-urlencoded") };
        //    var response3 = client.Post("https://yz.chsi.com.cn/zsml/mobi/query0Action.do", content, headers);

        //    if (response3.OK)
        //    {
        //        //Console.WriteLine(response3.GetString(Encoding.Unicode));
        //        keyValues = new List<KeyValue> {
        //            new KeyValue("pageno","1"),
        //             new KeyValue("mldm","zyxw"),
        //            new KeyValue("yjxkdm","0860")
        //        };
        //        content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyValues));
        //        response3 = client.Post("https://yz.chsi.com.cn/zsml/mobi/queryAction.do", content, headers);
        //        var result1 = response3.Parse<CHSI.SchoolResult>();
        //        if (result1 != null && result1.Flag)
        //        {
        //            int count = result1.Msg.TotalPage;
        //            for (int i = 1; i <= count; i++)
        //            {
        //                GetSchool(i, client, headers);
        //                Thread.Sleep(200);
        //            }
        //        }
        //    }
        //}

        //void GetSchool(int pageNo, IHttpClient client, Header[] headers)
        //{
        //    var keyValues = new List<KeyValue> {
        //            new KeyValue("pageno",pageNo.ToString()),
        //            new KeyValue("mldm","zyxw"),
        //            new KeyValue("yjxkdm","0860")
        //        };
        //    var content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyValues));
        //    var response3 = client.Post("https://yz.chsi.com.cn/zsml/mobi/queryAction.do", content, headers);
        //    var result1 = response3.Parse<CHSI.SchoolResult>();
        //    if (result1 != null && result1.Flag)
        //    {
        //        foreach (var item in result1.Msg.Schools)
        //        {
        //            // Console.WriteLine("{0},{1},{2},{3}", item.Dwdm, item.Dwmc, item.Ssmc, string.Join("|", item.Tag));
        //            GetSubject(item, client, headers);
        //            Thread.Sleep(200);
        //        }
        //    }
        //}
        //// https://yz.chsi.com.cn/zsml/mobi/querySchAction.do
        //void GetSubject(CHSI.School school, IHttpClient client, Header[] headers)
        //{
        //    var keyValues = new List<KeyValue> {
        //            new KeyValue("dwmc",school.Dwmc),
        //            new KeyValue("dwdm",school.Dwdm),
        //            new KeyValue("yjxkdm","0860")
        //        };
        //    var content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyValues));
        //    var response3 = client.Post("https://yz.chsi.com.cn/zsml/mobi/querySchAction.do", content, headers);
        //    var result1 = response3.Parse<CHSI.SubjectResult>();
        //    if (result1 != null && result1.Flag)
        //    {
        //        foreach (var item in result1.Msg.Schools)
        //        {
        //            // Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}", school.Dwdm, school.Dwmc, school.Ssmc, string.Join("|", school.Tag), item.Yxsmc, item.Zymc, item.Yjfxmc);
        //            item.Ssmc = school.Ssmc;
        //            item.Tag = school.Tag;

        //            GetExam(item, client, headers);

        //            Thread.Sleep(200);
        //        }
        //    }
        //}

        //void GetExam(CHSI.School school, IHttpClient client, Header[] headers)
        //{
        //    var response3 = client.Get($"https://yz.chsi.com.cn/zsml/mobi/kskmAction.do?id={school.Id}&t=164627167214");
        //    var result1 = response3.Parse<CHSI.ExamResult>();
        //    if (result1 != null && result1.Flag)
        //    {
        //        var item = result1.Msg.Exams;
        //        string ksfw = "";
        //        if (item.KsfwList != null && item.KsfwList.Any())
        //        {
        //            string json = JsonConvert.SerializeObject(item.KsfwList[0]);
        //            List<string> ks = new List<string>();
        //            var properties = JObject.Parse(json).Properties();
        //            foreach (var p in properties)
        //            {
        //                var val = p.Value.ToObject<CHSI.ExamValue>();
        //                ks.Add(val.Value);
        //            }

        //            ksfw = string.Join("|", ks);
        //        }
        //        Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", school.Dwdm, school.Dwmc, school.Ssmc, string.Join("|", school.Tag), school.Yxsmc, school.Zymc, school.Yjfxmc, ksfw);
        //    }
        //}
    }
}
